<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    //97.89%
    var numRookCaptures = function(board) {
        const len = board.length;
        let row = 0, col = 0;
        for(row=0; row<len; row++){
            if(board[row].indexOf('R') != -1){
                col = board[row].indexOf('R');
                break;
            }
        }
        let n = 0;
        let col1 = col;
        while(col1 > 0){
            col1--;
            if(board[row][col1] === 'p'){
                n++;
                break;
            }else if(board[row][col1] != '.')break;
        }
        let col2 = col;
        while(col2 < len-1){
            col2++;
            if(board[row][col2] === 'p'){
                n++;
                break;
            }else if(board[row][col2] != '.')break;
        }
        let row1 = row
        while(row1 > 0){
            row1--;
            if(board[row1][col] === 'p'){
                n++;
                break;
            }else if(board[row1][col] != '.')break;
        }
        let row2 = row;
        while(row2 < len-1){
            row2++;
            if(board[row2][col] === 'p'){
                n++;
                break;
            }else if(board[row2][col] != '.')break;
        }
        return n;
    };
    let board = [
        [".",".",".",".",".",".",".","."],
        [".",".",".","p",".",".",".","."],
        [".",".",".",".",".",".",".","."],
        [".",".",".","R",".","p","B","."],
        [".",".",".",".",".",".",".","."],
        [".",".",".","p",".",".",".","."],
        [".",".",".",".",".",".",".","."],
        [".",".",".",".",".",".",".","."]
    ];
    console.log(numRookCaptures(board));
    
</script>
</html>